local gongao = fk.CreateSkill{
  name = "reminiscence__gongao",
  tags = {Skill.Compulsory},
}

local gongao_mark = "@reminiscence__gongao"

Fk:loadTranslationTable{
  ["reminiscence__gongao"] = "功獒",
  [":reminiscence__gongao"] = "锁定技，当你执行以下效果后，你的手牌上限+1。"
  .."出牌阶段结束时，若你本阶段完成了执行了以下所有效果，你将手牌数摸至体力上限。"
  .."<br/>1.对体力值最大的其他角色造成伤害。"
  .."<br/>2.对手牌数最多的其他角色使用牌。",

  [gongao_mark] = "功獒",

  ["$reminiscence__gongao1"] = "恪尽职守，忠心事主。",
  ["$reminiscence__gongao2"] = "攻城拔寨，建功立业。",
}

gongao:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, gongao_mark, {"×", "×"})
end)

gongao:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(gongao.name)
    and player.phase == Player.Play
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, gongao_mark, {"×", "×"})
  end,
})

---@param player ServerPlayer
---@param n number
local function gongao_acconplish(player, n)
  local room = player.room
    room:addPlayerMark(player, MarkEnum.AddMaxCards)
  local mark = player:getTableMark(gongao_mark)
  mark[n] = "√"
  room:setPlayerMark(player, gongao_mark, mark)
end

gongao:addEffect(fk.DamageCaused, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if target and player == target and player:hasSkill(gongao.name) then
      local room = player.room
      return not table.find(room:getOtherPlayers(player, false), function (p)
        return p.hp > data.to.hp
      end)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    gongao_acconplish(player, 1)
  end
})

gongao:addEffect(fk.TargetSpecified, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if not data.to or data.to == target then return end
    if player == target and player:hasSkill(gongao.name) then
      local room = player.room
      return not table.find(room:getOtherPlayers(player, false), function (p)
        return p:getHandcardNum() > data.to:getHandcardNum()
      end)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    gongao_acconplish(player, 2)
  end,
})

gongao:addEffect(fk.EventPhaseEnd, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player == target and player:hasSkill(gongao.name) then
      return player.phase == Player.Play
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getMark(gongao_mark)
    room:setPlayerMark(player, gongao_mark, 0)
    return not table.contains(mark, "×")
  end,
  on_use = function (self, event, target, player, data)
    local n = player.maxHp - player:getHandcardNum()
    if n <= 0 then return end
    player:drawCards(n, gongao.name)
  end
})

return gongao